# coding=utf-8
"""
    @project: qabot
    
    @file： user.py
    @date：2023/9/4 10:57
    @desc:
"""
from django.core import cache
from xpack.serializers.system_params_serializers import SystemParamsSerializer
from drf_yasg.utils import swagger_auto_schema
from rest_framework.decorators import action
from rest_framework.views import APIView
from rest_framework.views import Request
from common.response import result

from django.utils.translation import gettext_lazy as _
from application.swagger_api.application_api import ApplicationApi
from common.constants.permission_constants import CompareConstants, PermissionConstants, Permission, Group, Operate, \
    ViewPermission, RoleConstants
from common.auth import TokenAuth, has_permissions

user_cache = cache.caches['user_cache']
token_cache = cache.caches['token_cache']

class SystemApiKey(APIView):
        authentication_classes = [TokenAuth]

        @action(methods=['POST'], detail=False)
        @swagger_auto_schema(operation_summary=_("Add ApiKey"),
                             operation_id=_("Add ApiKey"),
                             tags=[_('Application/API_KEY')],
                             manual_parameters=ApplicationApi.ApiKey.get_request_params_api())
        # @has_permissions(ViewPermission(
        #     [RoleConstants.ADMIN, RoleConstants.USER],
        #     [lambda r, keywords: Permission(group=Group.APPLICATION, operate=Operate.MANAGE)],
        #     compare=CompareConstants.AND))
        def post(self, request: Request):
            return result.success(
                SystemParamsSerializer.SystemApiKeySerializer(
                    data={'user_id': request.user.id}).generate())

        @action(methods=['GET'], detail=False)
        @swagger_auto_schema(operation_summary=_("Get the application API_KEY list"),
                             operation_id=_("Get the application API_KEY list"),
                             tags=[_('Application/API_KEY')],
                             manual_parameters=ApplicationApi.ApiKey.get_request_params_api()
                             )
        # @has_permissions(ViewPermission(
        #     [RoleConstants.ADMIN, RoleConstants.USER],
        #     [lambda r, keywords: Permission(group=Group.APPLICATION, operate=Operate.MANAGE)],
        #     compare=CompareConstants.AND))
        def get(self, request: Request):
            return result.success(SystemParamsSerializer.SystemApiKeySerializer(
                data={'user_id': request.user.id}).list())

        class Operate(APIView):
            authentication_classes = [TokenAuth]

            @action(methods=['PUT'], detail=False)
            @swagger_auto_schema(operation_summary=_("Modify application API_KEY"),
                                 operation_id=_("Modify application API_KEY"),
                                 tags=[_('Application/API_KEY')],
                                 manual_parameters=ApplicationApi.ApiKey.Operate.get_request_params_api(),
                                 request_body=ApplicationApi.ApiKey.Operate.get_request_body_api())
            # @has_permissions(ViewPermission(
            #     [RoleConstants.ADMIN, RoleConstants.USER],
            #     [lambda r, keywords: Permission(group=Group.APPLICATION, operate=Operate.MANAGE)],
            #     compare=CompareConstants.AND), PermissionConstants.APPLICATION_EDIT,
            #     compare=CompareConstants.AND)
            def put(self, request: Request, api_key_id: str):
                return result.success(
                    SystemParamsSerializer.SystemApiKeySerializer.Operate(
                        data={'user_id': request.user.id,
                              'api_key_id': api_key_id}).edit(request.data))

            @action(methods=['DELETE'], detail=False)
            @swagger_auto_schema(operation_summary=_("Delete Application API_KEY"),
                                 operation_id=_("Delete Application API_KEY"),
                                 tags=[_('Application/API_KEY')],
                                 manual_parameters=ApplicationApi.ApiKey.Operate.get_request_params_api())
            # @has_permissions(ViewPermission(
            #     [RoleConstants.ADMIN, RoleConstants.USER],
            #     [lambda r, keywords: Permission(group=Group.APPLICATION, operate=Operate.MANAGE)],
            #     compare=CompareConstants.AND), PermissionConstants.APPLICATION_DELETE,
            #     compare=CompareConstants.AND)
            def delete(self, request: Request, api_key_id: str):
                return result.success(
                    SystemParamsSerializer.SystemApiKeySerializer.Operate(
                        data={'user_id': request.user.id,
                              'api_key_id': api_key_id}).delete())

        